package com.neiru.divider.thread;

import java.util.ArrayList;
import java.util.List;

import android.os.Handler;

import com.neiru.divider.bean.Ball;
import com.neiru.divider.bean.Divider;
import com.neiru.divider.bean.Section;
import com.neiru.divider.constants.Axis;

public class SectionThread extends Thread {
	private Handler sectionHandler;
	private Section section;
	private Divider divider;
	
	private List<BallThread> ballThreads;
	private DividerThread dividerThread;
	
	public static int sectionThreadInstanceCounter = 0;
	
	public SectionThread(Handler sectionHandler, Section section) {
		this.sectionHandler = sectionHandler;
		this.section = section;
		this.ballThreads = new ArrayList<BallThread>();
	}
	public void run() {
		SectionThread.sectionThreadInstanceCounter++;
		for(Ball ball: section.getBalls()) {
			BallThread ballThread = new BallThread(section, ball, sectionHandler);
			ballThread.start();
			this.ballThreads.add(ballThread);
		}
	}
	public void setDivider(Divider divider) {
		this.divider = divider;
	}
	public void startDivider() {
		dividerThread = new DividerThread(sectionHandler);
		dividerThread.setSection(section);
		dividerThread.setDivider(divider);
		dividerThread.start();
		for(BallThread ballThread: ballThreads) {
			ballThread.setDivider(divider);
		}
	}
	
	public boolean hasDivider() {
		return this.divider != null;
	}
	
	public void killAllThreads() {
		for(BallThread ballThread: ballThreads) {
			ballThread.killAllThreads();
		}
		if(dividerThread != null) {
			dividerThread.killAllThreads();
		}
	}
	public Section getSection() {
		return section;
	}
	public void setSection(Section section) {
		this.section = section;
	}
	public List<BallThread> getBallThreads() {
		return ballThreads;
	}
	public void setBallThreads(List<BallThread> ballThreads) {
		this.ballThreads = ballThreads;
	}
	public Divider getDivider() {
		return divider;
	}
	
	public void addBallThread(BallThread ballThread) {
		this.ballThreads.add(ballThread);
	}
	
	public SectionThread divide() {
		if(divider != null) {
			Section newSection = new Section();
			SectionThread newSectionThread = new SectionThread(sectionHandler, newSection);
			newSection.setTop(section.getTop().intValue());
			newSection.setRight(section.getRight().intValue());
			newSection.setBottom(section.getBottom().intValue());
			newSection.setLeft(section.getLeft().intValue());
			List<Ball> toRemoveBalls = new ArrayList<Ball>();
			List<BallThread> toRemoveBallThreads = new ArrayList<BallThread>();
			if(Axis.AXIS_X.equals(divider.getAxis())) {
				newSection.setTop(divider.getStartPosition().getY().intValue());
				section.setBottom(divider.getStartPosition().getY().intValue());
				int i = 0;
				for(Ball ball: section.getBalls()) {
					// La bola esta debajo de la seccion 0, se pasara a la newSection
					if(ball.getActualPosition().getY().intValue() > section.getBottom()) {
						BallThread ballThread = ballThreads.get(i);
						toRemoveBalls.add(ball);
						toRemoveBallThreads.add(ballThread);
						
						newSection.addBall(ball);
						ballThread.setSection(newSection);
						newSectionThread.addBallThread(ballThread);
					}
					i++;
				}
			} else {
				newSection.setLeft(divider.getStartPosition().getX().intValue());
				section.setRight(divider.getStartPosition().getX().intValue());
				int i = 0;
				for(Ball ball: section.getBalls()) {
					// La bola esta debajo de la seccion 0, se pasara a la newSection
					if(ball.getActualPosition().getX().intValue() > section.getRight()) {
						BallThread ballThread = ballThreads.get(i);
						toRemoveBalls.add(ball);
						toRemoveBallThreads.add(ballThread);
						
						newSection.addBall(ball);
						ballThread.setSection(newSection);
						newSectionThread.addBallThread(ballThread);
					}
					i++;
				}
			}
			for(Ball remove: toRemoveBalls) {
				section.getBalls().remove(remove);
			}
			for(BallThread ballThread: toRemoveBallThreads) {
				this.ballThreads.remove(ballThread);
			}
			return newSectionThread;
		}
		return null;
	}
	
	
}
